package front_message_api

import (
	"blog_go/global"
	"blog_go/models"
	"blog_go/models/res"
	"blog_go/utils"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func (MessageApi) MessageList(c *gin.Context) {
	_claims, _ := c.Get(utils.Claims)
	claims := _claims.(*utils.CustomClaims)
	page := models.DefaultPageInfo()
	if err := c.ShouldBindQuery(page); err != nil {
		logrus.Error(err)
		panic(err)
	}
	var messageModelList []models.MessageModel
	var count int64
	global.DB.Model(&models.MessageModel{}).Count(&count)
	err := global.DB.Where("send_user_id = ? or rev_user_id = ?", claims.UserID, claims.UserID).
		Order(page.Sort).
		Offset(page.Limit * (page.Page - 1)).
		Limit(page.Limit).
		Find(&messageModelList).Error
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	res.OkWithList(messageModelList, count, c)
}
